package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonTrading;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTrading;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author : luoyong
 * @date : 2020-08-01 16:58
 **/
@Component(PtoNum.TRADINGRESPONSE + "")
public class TradingResponse implements ReceiveMsg{
    private final Logger logger = LoggerFactory.getLogger(TradingResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;
    @Override
    public void execute(Message msg) {
        GameTrading.TradingResponse tradingResponse = (GameTrading.TradingResponse) msg;
        int code = tradingResponse.getCode();
        switch (code){
            case CommonTrading.REQUEST:
                logger.info("收到一条来自 {} 的交易建立请求！", tradingResponse.getStringId());
                break;
            case CommonTrading.RESPONSE:
                logger.info("与 {} 的连接已建立！", tradingResponse.getStringId());
                userServiceCache.getUserInMap().get(clientHandler.selfId).setInTradingUserId(tradingResponse.getStringId());
                userServiceCache.getUserInMap().get(clientHandler.selfId).setTradingIdToTradingMap(new HashMap<>());
                userServiceCache.getUserInMap().get(tradingResponse.getStringId()).setInTradingUserId(clientHandler.selfId);
                userServiceCache.getUserInMap().get(tradingResponse.getStringId()).setTradingIdToTradingMap(new HashMap<>());
                break;
            case CommonTrading.CANCEL:
                logger.info("与 {} 的交易连接已关闭！", tradingResponse.getStringId());
                userServiceCache.getUserInMap().get(clientHandler.selfId).setInTradingUserId(null);
                userServiceCache.getUserInMap().get(clientHandler.selfId).setTradingIdToTradingMap(null);
                userServiceCache.getUserInMap().get(tradingResponse.getStringId()).setInTradingUserId(null);
                userServiceCache.getUserInMap().get(tradingResponse.getStringId()).setTradingIdToTradingMap(null);
                break;
            case CommonTrading.SEND_TRADING:
                GameTrading.Trading trading = tradingResponse.getTrading(0);
                logger.info("收到一条交易请求：{}", JsonFormat.printToString(trading));
                userServiceCache.getUserInMap().get(tradingResponse.getStringId()).getTradingIdToTradingMap().put(trading.getTradingId(), trading);
                break;
            case CommonTrading.RECEIVE_TRADING_AGREE:
                GameTrading.Trading trading1 = tradingResponse.getTrading(0);
                logger.info("收到一条交易完成的通知：{}", JsonFormat.printToString(trading1));
                userServiceCache.getUserInMap().get(trading1.getSenderId())
                        .getTradingIdToTradingMap().remove(trading1.getTradingId());
                break;
            case CommonTrading.RECEIVE_TRADING_DISAGREE:
                GameTrading.Trading trading2 = tradingResponse.getTrading(0);
                logger.info("收到一条交易被拒绝的通知：{}", JsonFormat.printToString(trading2));
                userServiceCache.getUserInMap().get(trading2.getSenderId())
                        .getTradingIdToTradingMap().remove(trading2.getTradingId());
                break;
            case CommonTrading.ERROR:
                logger.info("{}", tradingResponse.getStringId());
                break;
            default:
                break;
        }
    }
}
